<?php
class Arrange extends Controller 
{
	public function __construct($controller)
	{
		parent::__construct($controller);	
	}
	
	public function index()
	{
		if($thisUserInfo = $this->__isLogined())
		{
			$this->assign('thisUserInfo', $thisUserInfo);
			
			$config = Utils::cacheRead('config.php', ARRANGE_PATH); //配置文件
			$this->assign('config', $config);
			
			$staff_info = Utils::cacheRead('staff-info.php', ARRANGE_PATH); //用户列表读了两次 staff_info.php 鸡肋
			$this->assign('staff_info', $staff_info); //
			
			if(isset($_REQUEST['id']))
			{
				$active_item = Utils::cacheRead('item-'.$_REQUEST['id'].'.php', ARRANGE_PATH);
			}
			else
			{
				$active_item = Utils::cacheRead($config['active-item'], ARRANGE_PATH); //当前激活的项目
			}
		
			$this->assign('active_item', $active_item);
			
			$have_select_num = count($active_item['result']);
			$this->assign('have_select_num', $have_select_num);
			
			$sort_item_array = $active_item['result'];
			usort($sort_item_array, array($this, '__arraySort'));
			$this->assign('sort_item_array', $sort_item_array);
			
			
			$files = Helper_Folder::find(ARRANGE_PATH, '#.*item-\d\.php#', 'name');
			
			$items = array(); //所有排号项目
			foreach ($files as $path)
			{
				$filename = basename($path);
				$item_config = Utils::cacheRead($filename , ARRANGE_PATH);
				$items[$filename] = $item_config;
			}
			$this->assign('items', $items);
			
			
			$have_select_num = count($active_item['result']);
			$this->assign('have_select_num', $have_select_num);
			
			$last_num = $config['total-num']-$have_select_num;
			$this->assign('last_num', $last_num);
			
			$this->display('Arrange/index.html');
		}
		else 
		{
			$this->showMessage('请先登录！', SITE_URL.'arrange/login');
		}
	}
	
	public function login() 
	{
		if($this->__isLogined())
		{
			$this->redirectByHeader(SITE_URL.'arrange/index');
			exit;
		}
		
		if($this->isPostRequest())
		{
			$staff_id = $_REQUEST['staff_id'];
			$password = $_REQUEST['password'];
			$cookieTime = empty($_REQUEST['cookietime']) ? 86400 : $_REQUEST['cookietime'];
			$staffInfo = $this->__findStaffViaStaffId($staff_id);
			if($staffInfo)
			{
				if($staffInfo['password'] != $password) {
					$this->showMessage('密码错误！', 'back');
				}
				
				$password = $staffInfo['password'];
				$cookie =  Factory::cookie();
				$cookie->set('auth', Utils::stringEncode($staff_id."\t".$staffInfo['name']."\t".$password."\t".$staffInfo['groupid'], Config::get('config', 'authKey')), $cookieTime);
				$cookie->set('staff_id', $staff_id, $cookieTime);

				$this->redirectByHeader(SITE_URL.'arrange/index');
			}
			else
			{
				$this->showMessage('账号不存在!', 'back');
			}
		}
		else
		{
			$this->display('Arrange/login.html');
		}
	}
	
	public function logout()
	{
		$cookie =  Factory::cookie();
		$cookie->set('auth');
		$cookie->set('staff_id');
		$this->showMessage('退出成功！', SITE_URL.'arrange/login', 1000, true);
	}
	
	public function save()
	{
		if(!$this->isAjaxRequest())
		{
			$this->showmessage('You should not be here!');	
		}
		
		$thisStaffInfo = $this->__isLogined();  //登录检测
		if(!$thisStaffInfo)
		{
			return array('state'=>false, 'message'=>'请先登录！');
		}
		
		$num = $_REQUEST['num'];
		$id = $_REQUEST['id'];
		
	
		$this_item = Utils::cacheRead('item-'.$id.'.php', ARRANGE_PATH); //当前激活的项目
		
		//检查num 是否已被选
		$selected = false;
		foreach($this_item['result'] as $results ) 
		{
			if($results['number'] == $num)
			{
				return array('state' => false, 'msg'=>'对不起此号码已被抢！摇号程序将继续运行..');
			}
		}
		
		
		$newDataArray = array(
		'staff_id'=>$thisStaffInfo['staff_id'],
		'number'=>$num,
		'get-time'=>TIME
		);
		$this_item['result'][$thisStaffInfo['staff_id']] = $newDataArray;   //更新数据
		if(Utils::cacheWrite('item-'.$id.'.php', $this_item, ARRANGE_PATH))
		//if(true)
		{
			$newDataArray['name'] = $thisStaffInfo['name'];
			$newDataArray['get-time'] = date('m-d-H:i', $newDataArray['get-time']);
			return array('state'=> true, 'data'=>$newDataArray);
		}
		else 
		{
			return array('state'=>false,'msg'=>'数据保存出错！');
		}
	}
	
	private function __isLogined()
	{
		$cookie =  Factory::cookie();
		$encodeString = $cookie->get('auth');
		if ($encodeString) 
		{
			list($staff_id, $name, $password, $groupid) = explode("\t", Utils::stringDecode($encodeString, Config::get('config', 'authKey')));
			return compact('staff_id','name' ,'password', 'groupid');
		}
		else 
		{
			return false;
		}
		
	}
	
	private function __findStaffViaStaffId($staff_id)
	{
		$staff_array = Utils::cacheRead('staff-info.php', ARRANGE_PATH);
		if(key_exists($staff_id, $staff_array)) 
		{
			return $staff_array[$staff_id];
		}
		else 
		{
			return false;
		}
	}

	private function __arraySort($a, $b)
	{
		if ($a['number'] == $b['number']) {
			return 0;
		}
		return ($a['number'] < $b['number']) ? -1 : 1;
	}


	
}